import statService from '@/api/stat/stat'

let data = new class {
  // 各状态数据统计

  constructor() {
    // this.xAxisData = ['<10M','10-50M','50-100M','100-500M','>500M',];
    this.yAxisData_1 = [];
    this.yAxisData_2 =[];
    this.data_1=[];
    this.data_2=[];

  }
  setConfig(param){
    // 为整个类设置个性化参数
    return this
  }

  async getData() {
    let {success,data}=await statService.queryStatusCount()
    if(success){
      return {data}
    }else{
      throw new Error('获取数据失败')
    }
  }
  async formatData(){
    let {data}=await this.getData()
    let formatData={
      data_1:[],
      data_2: []
    }
    _.forEach(data,function (v,i) {
      if(!v.count){
        v.count=0
      }
    })
    formatData.data_1=_.filter(data,function (v) {
      return v.code==4 || v.code==-4;
    })
    formatData.data_2=_.filter(data,function (v) {
      return [1, 2, 3].includes(v.code)
    })
    return {formatData}

  }
  async createData(){
    // let countIn = await this.countIn();
    // let countOut = await this.countOut();
    let {formatData}=await this.formatData();
    let me = this
    let arr1Sum=0;
    let arr2Sum=0;
    me.yAxisData_1 = [];
    me.yAxisData_2 = [];
    me.data_1 = [];
    me.data_2 = [];

    _.forEach(formatData.data_1,function (v,i) {
      arr1Sum += parseInt(v.count);
    })
    _.forEach(formatData.data_2,function (v,i) {
      arr2Sum += parseInt(v.count);
    })
    _.forEach(formatData.data_1,function (v,i) {
      let value = '';
      if(arr1Sum==0){
        value=`${v.name}  ${v.count}条   0% `
      }else {
        value=`${v.name}  ${v.count}条   ${Math.round(v.count/arr1Sum*10000)/100}% `
      }
      let json={
        value:value,
        textStyle:{
          color:"rgba(170,227,255,1)"
        }
      }
      me.yAxisData_1.push(json)
    })
    _.forEach(formatData.data_2,function (v,i) {
      let value = '';
      if(arr2Sum==0){
        value=`${v.name}  ${v.count}条   0% `
      }else {
        value=`${v.name}  ${v.count}条   ${Math.round(v.count/arr2Sum*10000)/100}% `
      }
      let json={
        value:value,
        textStyle:{
          color:"rgba(170,227,255,1)"
        }
      }
      me.yAxisData_2.push(json)
    })
    me.data_1=_.map(formatData.data_1,'count')
    me.data_2=_.map(formatData.data_2,'count')
    return {
      "yAxisData_1": this.yAxisData_1,
      "yAxisData_2": this.yAxisData_2,
      "data_1":this.data_1,
      "data_2":this.data_2
    };
  }
}

export {
  data
}
